package com.ihighest.app.infox.activity;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.MyCollectionAdapter;
import com.ihighest.app.infox.myview.ActivityTitle;

/**
 * 我的收藏界面
 * 
 * @author lkx Created at 2015-10-13 上午10:28:05
 * @version 1.0
 */
public class MyCollectionActivity extends BaseActivity {
	private ActivityTitle title;
	private PullToRefreshListView collectionListview;
	private MyCollectionAdapter adapter;
	int page = 1;
	private TextView noCollection;
	long time;
	private JSONArray array;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_mycollection);
		initView();
	}

	/**
	 * 初始化绑定
	 * 
	 * 
	 * @author lkx Created at 2015-10-13 上午10:12:02
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void initView() {
		title = (ActivityTitle) findViewById(R.id.mActivityTitle);
		title.setTitle("我收藏的");
		title.setViewVersible(title.titleBack);
		title.setViewGone(title.titleMsg, title.titleRefresh, title.rightLayout);
		collectionListview = (PullToRefreshListView) findViewById(R.id.minelistView);
		noCollection = (TextView) findViewById(R.id.noCollection);
		initData();
		collectionListview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				page = 1;
				initData();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				page++;
				initData();

			}

		});
		collectionListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				String itemid = null;
				try {
					itemid = array.getJSONObject(position - 1).getString("id");
					int type = array.getJSONObject(position - 1).getInt("type");
					switch (type) {
					case 1:
						Intent intent = new Intent(MyCollectionActivity.this, InformationalContentActivity.class);

						// intent.putExtra("newsId",
						// array.getJSONObject(position -
						// 2).getString("id"));
						intent.putExtra("newsId", itemid);
						startActivity(intent);
						break;

					case 2:
						Intent intent2 = new Intent(MyCollectionActivity.this, InformationalContentActivity.class);

						// intent.putExtra("newsId",
						// array.getJSONObject(position -
						// 2).getString("id"));
						intent2.putExtra("newsId", itemid);
						startActivity(intent2);
						break;
					case 3:
						Intent intent3 = new Intent(MyCollectionActivity.this, InformationalContentActivity.class);

						// intent.putExtra("newsId",
						// array.getJSONObject(position -
						// 2).getString("id"));
						intent3.putExtra("newsId", itemid);
						startActivity(intent3);
						break;
					case 4:

						break;

					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
	}

	/**
	 * 获取数据
	 * 
	 * 
	 * @author lkx Created at 2015-10-26 上午9:21:56
	 */
	private void initData() {
		ApiParam param = ApiParam.create();
		param.addParam("page", page);

		if (page >= 2) {
			param.addParam("time", 0);
		}
		Api.get("me/favorites", param, new ApiCallback(MyCollectionActivity.this) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getDatas() == null || result.getDatas().length() == 0) {
					if (page == 1) {
						noCollection.setVisibility(View.VISIBLE);
						collectionListview.setVisibility(View.GONE);
					} else {
						page--;
						ToastKit.showShort(MyCollectionActivity.this, "数据加载完毕");
					}

				} else {
					noCollection.setVisibility(View.GONE);
					collectionListview.setVisibility(View.VISIBLE);
					if (page == 1) {
						array = result.getDatas();
						adapter = new MyCollectionAdapter(MyCollectionActivity.this, result.getDatas());
						collectionListview.setAdapter(adapter);
					} else {

						try {
							array = AppKit.joinJSONArray(array, result.getDatas());
						} catch (JSONException e) {
							e.printStackTrace();
						}
						adapter.appendData(result.getDatas());
					}
					time = result.getTime();

				}
				collectionListview.onRefreshComplete();
			}

			@Override
			public void handleFailure(Exception e, String msg) {
				collectionListview.onRefreshComplete();

			}
		}, MyCollectionActivity.this);

	}
}
